var t = getApp(), n = 0;

Page({
    data: {
        fridayHidden: !1,
        plusHidden: !0,
        fridayPage:0,
        plusPage:0,
        notPlusHidden:1,
        notPlusHide:1,
        fridayGoods:[],
        plusGoods:[]
    },
    onLoad: function() {
        wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: '#F86B23',
            animation: {
                duration: 400,
                timingFunc: 'easeIn'
            }
        });
        this.user();
        this.getMore();
        this.getMorePlus();
    },
    user: function() {
        var e = this, o = wx.getStorageSync("token");
        wx.request({
            url: t.apiUrl("user/info"),
            data: {
            },
            header: {
                "Content-Type": "application/json",
                "X-ECTouch-Authorization": o
            },
            method: "POST",
            success: function(res) {
               if(res.data.data.user_rank == 12){
                   e.setData({
                       notPlusHidden:1
                   })
               }else{
                e.setData({
                    notPlusHidden:0
                })
               }
            }
        });
    },
    getMore:function () {
        wx.showLoading({
            title: '加载中',
            mask: true,
            success: (result)=>{
                
            },
            fail: ()=>{},
            complete: ()=>{}
        });
        var that = this;
        var page = that.data.fridayPage;
        that.setData({
            fridayPage:++page
        });
        var token = wx.getStorageSync("token");
        wx.request({
            url: t.apiUrl("activity/fridayGoods"),
            method: "post",
            header: {
                "Content-Type": "application/json",
                "X-ECTouch-Authorization": token
            },
            data:{
                page:page,
                pageSize:6
            },
            success: function (res) {
                var data = that.data.fridayGoods;
                if(res.data.data.length != undefined){
                    that.setData({
                        fridayGoods:data.concat(res.data.data)
                    });
                }
                wx.hideLoading();
                  
            }
        });
    },
    buyFridayGoods:function(e){
        var id =e.currentTarget.id;
        var index = e.currentTarget.dataset.index;
        var token = wx.getStorageSync("token");
        var that =this;
        if(that.data.notPlusHidden == 0){
            that.setData({
                notPlusHide:0
            })
            return;
        }
        wx.request({
            url: t.apiUrl("cart/add"),
            method: "post",
            header: {
                "Content-Type": "application/json",
                "X-ECTouch-Authorization": token
            },
            data:{
                id:id,
                extension_code:'friday',
                num:1,
                attr_id:JSON.stringify([])
            },
            success: function (res) {
                if(res.data.data.code == 0){
                    var key = 'fridayGoods[' + index + '].cart_exist'
                    that.setData({
                        [key]:1
                    });
                }else{
                    wx.showToast({
                        title: res.data.data.msg,
                        icon: 'none',
                        image: '',
                        duration: 2000,
                        mask: true,
                        success: (result) => {
                            
                        },
                        fail: () => {},
                        complete: () => {}
                    });
                }

            }
        });
    },
    buyPlusGoods:function(e){
        var id =e.currentTarget.id;
        var index = e.currentTarget.dataset.index;
        var token = wx.getStorageSync("token");
        var that =this;
        if(that.data.notPlusHidden == 0){
            that.setData({
                notPlusHide:0
            })
            return;
        }
        wx.request({
            url: t.apiUrl("cart/add"),
            method: "post",
            header: {
                "Content-Type": "application/json",
                "X-ECTouch-Authorization": token
            },
            data:{
                id:id,
                extension_code:'free',
                num:1,
                attr_id:JSON.stringify([])
            },
            success: function (res) { 
                if(res.data.data.code == 0){
                    var key = 'plusGoods[' + index + '].cart_exist'
                    that.setData({
                        [key]:1
                    });
                }else{
                    wx.showToast({
                        title: res.data.data.msg,
                        icon: 'none',
                        image: '',
                        duration: 2000,
                        mask: true,
                        success: (result) => {
                            
                        },
                        fail: () => {},
                        complete: () => {}
                    });
                      
                }

            }
        });
    },
    getMorePlus:function () {
        var that = this;
        var page = that.data.plusPage;
        that.setData({
            plusPage:++page
        });
        
        var token = wx.getStorageSync("token");
        wx.request({
            url: t.apiUrl("activity/plusGoods"),
            method: "post",
            header: {
                "Content-Type": "application/json",
                "X-ECTouch-Authorization": token
            },
            data:{
                page:page,
                pageSize:6
            },
            success: function (res) {
                var data = that.data.plusGoods;
                if(res.data.data.length != undefined){
                    that.setData({
                        plusGoods:data.concat(res.data.data)
                    });
                }
                /* that.setData({
                    plusGoods:res.data.data
                }); */
            }
        });
    },
    tabClick:function(e){
        var that = this;
        var id = e.currentTarget.id;
        if(id == 'friday'){
                that.data.fridayHidden===true && that.setData({
                fridayHidden: false,
                plusHidden: true
            });
        }else{
            that.data.plusHidden===true  && that.setData({
                fridayHidden: true,
                plusHidden: false
            });
        }
        console.log(e)
    },
    buyPlus:function () {
        var that = this;
        that.setData({
          //  notPlusHidden:true
        });
        
        wx.navigateTo({url:"../user/plus_buy"});
    }
});